package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.DiseaseTypeDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("DisableLevel")
public class DisableLevelController extends BaseController{


    @Autowired
    @Qualifier("DisableLevelServiceImplOne")
    private DisableLevelService disableLevelService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;

    @Autowired
    @Qualifier("InstitutionServiceImplOne")
    private InstitutionService institutionService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;



    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("name");
            String staffCode = jsonObject.getString("staffCode");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ElderlyClass/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String insCode = institutionStaffRel.getInstitutionCode();


            DisableLevel disableLevel =new DisableLevel();
            disableLevel.setDisableLevelCode(UUID.randomUUID().toString());
            disableLevel.setName(name);
            disableLevel.setSort(0);
            disableLevel.setStatus(1);
            disableLevel.setInstitutionCode(insCode);
            disableLevelService.insert(disableLevel);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);

        } catch (Exception e) {
            insertError("/ElderlyClass/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int sort = jsonObject.getInteger("sort");
            int status = jsonObject.getInteger("status");

            String disableLevelCode =jsonObject.getString("disableLevelCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ElderlyClass/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            DisableLevel disableLevel =disableLevelService.getByDisableLevelCode(disableLevelCode);

            disableLevel.setName(name);
            disableLevel.setSort(sort);
            disableLevel.setStatus(status);
            disableLevelService.update(disableLevel);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ElderlyClass/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult getList(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ElderlyClass/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String insCode = institutionStaffRel.getInstitutionCode();

            List<DisableLevel> listElderlyClass = disableLevelService.listByInsCode(insCode);

            return success(listElderlyClass);
        } catch (Exception e) {
            insertError("/ElderlyClass/List",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ElderlyClass/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String insCode = institutionStaffRel.getInstitutionCode();

            PageInfo<DisableLevel> elderlyClassPageInfo =disableLevelService.pageByInsCode(pageIndex,pageSize,insCode);

            return success(elderlyClassPageInfo);
        } catch (Exception e) {
            insertError("/ElderlyClass/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "GovPage",method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ElderlyClass/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<DisableLevel> pageInfo =disableLevelService.pageByGovCode(pageIndex,pageSize,staff.getGovernmentCode());


            return success(pageInfo);
        } catch (Exception e) {
            insertError("/ElderlyClass/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



}
